﻿using System;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace PO_2011
{
    class PNM_Writer
    {
        private static Encoding encoding;

        public static void saveImage(Image image, PNM_Reader img, String file_name, String new_type, bool ASCII)
        {
            if (ASCII)
            {
                new_type = "P" + new_type;
                encoding = Encoding.ASCII;
            }
            else
            {
                new_type = "P" + ((Int32.Parse(new_type) + 3).ToString());
                encoding = Encoding.GetEncoding(28591);
            }

            File.Delete(file_name);

            String image_to_string = "";

            image_to_string += new_type + "\n";
            image_to_string += "#WERSJA LIGHT GIMP :) \n";
            image_to_string += img.Header.Width + " " + img.Header.Height + "\n";

            if (new_type != "P1" && new_type != "P4")
            {
                image_to_string += img.Header.Depth + "\n";
            }

            File.Delete(file_name);

            using (StreamWriter outfile = new StreamWriter(file_name, true, encoding))
            {
                outfile.Write(image_to_string);
                outfile.Close();
            }

            StreamWriter MyStreamWriter = new StreamWriter(file_name, true, encoding);
            Bitmap image_bitmap = new Bitmap(image);

            // pierwsza pozycja to suma dajaca byte, drugi to do jakiej potegi podniesc 2;

            // getPixel(0, 0) lewy gorny naroznik
            Color color;
            int[] int_char = new int[2] { 0, 7 };


/*****************************************************/
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    color = image_bitmap.GetPixel(x, y);

                    /**************ASCII**************************/
                    if (new_type == "P1" | new_type == "P2" | new_type == "P3")
                    {
                        if (new_type == "P1")
                        {
                            MyStreamWriter.Write(ColorConvert.Colors.convertFromColorToBlack(color, new_type, img.Header.MagicNumber));
                            MyStreamWriter.Write("\n");
                        }
                        else if (new_type == "P2")
                        {
                            MyStreamWriter.Write(ColorConvert.Colors.convertFromColorToGray(color, new_type, img.Header.MagicNumber));
                            MyStreamWriter.Write("\n");
                        }
                        else if (new_type == "P3")
                        {
                            MyStreamWriter.Write(color.R);
                            MyStreamWriter.Write("\n");
                            MyStreamWriter.Write(color.G);
                            MyStreamWriter.Write("\n");
                            MyStreamWriter.Write(color.B);
                            MyStreamWriter.Write("\n");
                        }
                    }

                    /****************BINARY*************************************/
                    else
                    {
                        // P4 wczesniej jest warunek
                        if (new_type == "P4")
                        {
                            if (ColorConvert.Colors.convertFromColorToBlack(color, new_type, img.Header.MagicNumber) == '1')
                            {
                                int_char[0] += (Int16)Math.Pow(2, int_char[1]);
                            }
                            int_char[1] -= 1;

                            // jesli jest juz bajt, to zapisz bajt do pliku
                            if (int_char[1] == -1)
                            {
                                MyStreamWriter.Write(Convert.ToChar(int_char[0]));
                                int_char[0] = 0;
                                int_char[1] = 7;
                            }
                        }
                        else if (new_type == "P5")
                        {
                            MyStreamWriter.Write(ColorConvert.Colors.convertFromColorToGray(color, new_type, img.Header.MagicNumber));
                        }
                        else if (new_type == "P6")
                        {
                            MyStreamWriter.Write((Char)color.R);
                            MyStreamWriter.Write((Char)color.G);
                            MyStreamWriter.Write((Char)color.B);
                        }
                    } // P4, P5, P6

                } //for (int x = 0; x < image.Width; x++)
            } // for (int y = 0; y < image.Height; y++)

            if (new_type == "P4" && int_char[1] < 7)
            {
                MyStreamWriter.Write(Convert.ToChar(int_char[0]));
            }

            MyStreamWriter.Close();
                    
            MessageBox.Show("zapisano");
        }
    }
}
